package com.mali.user.aop;

import com.mali.common.annotation.OperationLog;
import com.mali.common.dto.Result;
import com.mali.common.util.UserContext;
import com.mali.common.web.domain.dto.ResetPwdDTO;
import com.mali.mq.template.LogTemplate;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

@Aspect
@Component
@Slf4j
public class LogAspect {

    private final LogTemplate logTemplate;

    public LogAspect(LogTemplate logTemplate) {
        this.logTemplate = logTemplate;
    }

    @Around("@annotation(operationLog)")
    public Object around(ProceedingJoinPoint joinPoint, OperationLog operationLog) throws Throwable {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        String methodName = signature.getMethod().getName();

        // 获取方法参数
        Object[] args = joinPoint.getArgs();
        Long operatorId = UserContext.getUserId();
        Long targetUserId = null;

        // 尝试从参数中获取userId
        for (Object arg : args) {
            if (arg instanceof ResetPwdDTO) {
                targetUserId = ((ResetPwdDTO) arg).getUserId();
                break;
            }
        }

        try {
            log.info("[{}]-[{}]-[开始] 方法: {}",
                    operationLog.module(), operationLog.operation(), methodName);

            // 执行原方法
            Object result = joinPoint.proceed();

            if (result instanceof Result<?> res) {
                if (res.success()) {
                    log.info("[{}]-[{}]-[成功]",
                            operationLog.module(), operationLog.operation());
                    logTemplate.sendSuccessLog(
                            operationLog.serviceName(),
                            operationLog.module(),
                            operationLog.operation(),
                            operatorId,
                            targetUserId,
                            "操作成功");
                } else {
                    log.warn("[{}]-[{}]-[失败] 原因: {}",
                            operationLog.module(), operationLog.operation(), res.getMessage());
                    logTemplate.sendFailLog(
                            operationLog.serviceName(),
                            operationLog.module(),
                            operationLog.operation(),
                            operatorId,
                            targetUserId,
                            res.getMessage());
                }
            }

            return result;
        } catch (Exception e) {
            log.error("[{}]-[{}]-[异常] 方法: {}, 异常: {}",
                    operationLog.module(), operationLog.operation(), methodName, e.getMessage());
            logTemplate.sendFailLog(
                    operationLog.serviceName(),
                    operationLog.module(),
                    operationLog.operation(),
                    operatorId,
                    targetUserId,
                    e.getMessage());
            throw e;
        }
    }
}